//
//  StatusViewModel.swift
//  weibo
//
//  Created by 张宏芝 on 15/9/5.
//  Copyright © 2015年 kama. All rights reserved.
//

import UIKit

 ///  原创模型的可重用标识符
let HMStatusNormalCellID = "HMStatusNormalCellID"
 ///  转发微博的可重用标识符
let HMStatusForwardCellID = "HMStatusForwardCellID"

class StatusViewModel: NSObject {
    
    /// 微博对象
    var status:Status
    /// 当前模型对应的行高
    var rowHeigth:CGFloat = 0
    /// 返回当前视图模型对应的可重用标识符
    var cellID:String{
        return status.retweeted_status != nil ? HMStatusForwardCellID : HMStatusNormalCellID
    }
    
    
/// 被转发的原创微博文字
    var forwardText:String? {
        let username = status.retweeted_status?.user?.name ?? ""
        let text = status.retweeted_status?.text ?? ""
        return "@\(username):\(text)"
    }
//    用户头像
    var userIconUrl:NSURL?{
        return NSURL(string: status.user?.profile_image_url ?? "")
    }
    
//    vip图片
/// 认证类型 -1：没有认证，0，认证用户，2,3,5: 企业认证，220: 达人
    var userVipImage:UIImage? {
        switch (status.user?.verified ?? -1) {
        case 0: return UIImage(named: "avatar_vip")
        case 2, 3, 5: return UIImage(named: "avatar_enterprise_vip")
        case 220: return UIImage(named: "avatar_grassroot")
        default: return nil
    }
}
    /// 会员等级 1-6
    var userMemberImage: UIImage? {
        if status.user?.mbrank > 0 && status.user?.mbrank < 7 {
            return UIImage(named: "common_icon_membership_level\(status.user!.mbrank)")
        }
        return nil
}
    var thumbnailURLs: [NSURL]?
//    将bmiddleURLs设置为计算型属性:将thumbnailURLs换位bmiddleURLs
//    因为只有用户在点击一张图像的时候,才会将缩略的图片临时生成中等尺寸的图像数组
    var bmiddleURLs:[NSURL]?{
//        1.判断thumbnailURLs是否为nil
        guard let urls = thumbnailURLs else{
            return nil
        }
//        2.顺序替换每一个url字符串中得单词
        var array = [NSURL]()
        for url in urls{
            let urlString = url.absoluteString.stringByReplacingOccurrencesOfString("/thumbnail/", withString: "/bmiddle/")
            array.append(NSURL(string: urlString)!)
        }
        return array
    }
        
//    构造函数
    init(status:Status){
        

        self.status = status
        
        if let urls = status.retweeted_status?.pic_urls ?? status.pic_urls {
            thumbnailURLs = [NSURL]()
            
            for dict in urls {
                thumbnailURLs?.append(NSURL(string: dict["thumbnail_pic"]!)!)
            }
        }
        super.init()
    }
    override var description: String {
        return status.description + " 缩略图 URL 数组 \(thumbnailURLs)"
    }
    

}















